<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>绑卡管理</title>
    <style type="text/css">
    </style>
    <jsp:include page="../../import.jsp" flush="true"/>
    <script type="text/javascript">
        var grid = null;
        var addGSData_s = {};
        $(function () {
            $("#searchBar").ligerForm();
            $("#ligergrid1").ligerGrid({
                url: '<%=path%>/custpay/cmmBindingCart/initCmmBindingCartList.do',
                method : 'get',
                parms:getSearchParams(),
                columns: [
                    { display: '开户名称', name: 'accNm', width: 100, align: 'center', enabledSort:true},
                    { display: '预留手机号', name: 'cardTel', width: 100, align: 'center', enabledSort:true},
                    { display: '银行账号', name: 'accNo', width: 150, align: 'center', enabledSort:true},
                    { display: '证件类型', name: 'certTypCd', width: 80, align: 'center', enabledSort:true},
                    { display: '证件号', name: 'certNo', width: 150, align: 'center', enabledSort:true},
                    { display: '银行名称', name: 'bkCdNm', width: 150, align: 'center', enabledSort:true},
                    { display: '银行编码', name: 'bkCd', width: 80, align: 'center', enabledSort:true},
/*
                    { display: '签约类型', name: 'payTypeName', width: 200, align: 'center', enabledSort:true},
*/
                    { display: '自动代扣首次设置时间', name: 'crtOlAutoDate', width: 150, align: 'center', render: renderDate},
                    {
                        display: '操作',width: 250,align: 'left',
                        render: function (rowdata, rowindex, value){
                            var h3 = '';
                            h3 += '<a class="sq-button-hasicon" href="javascript:change(' + "'" + rowdata.id + "'" + ')"><i class="sqBtnIco_4"></i>修改</a>';
                            h3 += '<a class="sq-button-hasicon" href="javascript:del(' + "'" + rowdata.id + "'"+ ",'" + rowdata.cstId + "'" + ')"><i class="sqBtnIco_7"></i>删除</a>';

                            if(rowdata.isAuto == 0){//当前未开启状态
                                h3 += '<a class="sq-button-hasicon" href="javascript:autoCharge(' + "'" + rowdata.id + "'"+ ",'" + rowdata.cstId + "'"+ ",'1'" + ')"><i class="sqBtnIco_15"></i>开启自动代扣</a>';
                            }else if(rowdata.isAuto == 1){//当前开启状态
                                h3 += '<a class="sq-button-hasicon" href="javascript:autoCharge(' + "'" + rowdata.id + "'"+ ",'" + rowdata.cstId + "'"+",'0'" + ')"><i class="sqBtnIco_14"></i>取消自动代扣</a>';
                            }
                            return h3;
                        }
                    }
                ],
                pageSize:10,
                root:'rows',
                record:'total',
                hideLoadButton : true,
                width: '100%',
                height:'100%',
                heightDiff:-2,
                checkbox:true,
                allowUnSelectRow: true,
                rownumbers:false,
                onBeforeShowData: function ()
                {
                    var grid = this;
                    grid.collapsedRows = [];
                }/*,
                detail: {
                    height: 'auto',
                    onShowDetail: showDetail_grid
                }*/
            });
            grid = liger.get("ligergrid1");

            $("#searchBtn").click(function(event) {
                var form = new liger.get("searchBar");
                var data = form.getData();
                console.log(data);
                for (name in data){
                    grid.setParm(name,data[name]);
                };
                grid.reload();
            });
            $("#clearBtn").click(function(event) {
                clearLigerForm("searchBar");
            });
            $("#dateform").ligerForm();

            //编辑保存
            $("#saveChange_Btn").click(function (event) {

                var form = liger.get("change_form");

                if (!form.valid()) {
                    $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                    return false; //验证不通过，不执行以下代码
                }

                var data = form.getData();
                $.ligerDialog.hide();
                //以下应该有一段Ajax交互
                var wait_dialog;
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    data: data,
                    url: '<%=path%>/custpay/cmmBindingCart/modify.do?id=' + addGSData_s.id+"&cstId="+addGSData_s.cstId,
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                    },
                    success: function (msg) {
                        wait_dialog.close();
                        if (msg.success == 1) {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                            bankHeadQ = null;
                            $("#resetBtn").click();
                            $("#searchBtn").click();
                        } else {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                        }
                    }
                });
            });
        });

        //查询参数
        function getSearchParams(){
            var form = new liger.get("searchBar");
            var data = form.getData();
            return data;
        }

        //修改事件
        function change(_id) {
            $.ajax({
                type: 'get',
                dataType: 'json',
                url: '<%=path%>/custpay/cmmBindingCart/getById.do?id=' + _id,
                success: function (msg) {
                    if (msg.success == 1) {
                        //$.ligerDialog.success(msg.info)
                        openChange();//弹出修改框
                        setData(msg.data);//设置数据
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        }
        function openChange() {
            $.ligerDialog.open({
                width: 450,
//            top: 100,
                title: '修改客户开户银行信息',
                target: $("#change_Html"),
                onHideDialog: function () {
                    clearLigerForm("change_form");
                }
            });
            var ligerForm = $("#change_form").ligerForm({validate: true});
        }
        //设置页面数据
        function setData(data) {
            var form = liger.get("change_form");
            addGSData_s.id = data.id;
            addGSData_s.cstId = data.cstId;
            form.setData({
                accNm: data.accNm,
                cardTel: data.cardTel,
                certNo: data.certNo
            });
        }

        //删除
        function del(_id,_cstId) {
            if (!_id)return;
            $.ligerDialog.confirm('确定删除吗?', function (flag) {
                if (flag == false)return;
                var wait_dialog;
                $.ajax({
                    type: 'get',
                    dataType: 'json',
                    url: '<%=path%>/custpay/cmmBindingCart/removeById.do?id=' + _id+"&cstId="+_cstId,
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                    },
                    success: function (msg) {
                        wait_dialog.close();
                        if (msg.success == 1) {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                            grid.reload($('.pcontrol input', grid.toolbar).val());
                        } else {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                        }
                    }
                });
            });
        }
        function autoCharge(_id,_cstId,_isAuto){
            if (!_id)return;
            $.ligerDialog.confirm('确定该操作吗?', function (flag) {
                if (flag == false)return;
                var wait_dialog;
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    url: '<%=path%>/custpay/cmmBindingCart/autoChargeOperate.do',
                    data:{"id":_id,"cstId":_cstId,"isAuto":_isAuto},
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                    },
                    success: function (msg) {
                        wait_dialog.close();
                        if (msg.success == 1) {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                            grid.reload($('.pcontrol input', grid.toolbar).val());
                        } else {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                        }
                    }
                });
            });
        }

        /**
         * 选发短信
         */
        function sendSMS() {
            var selectedRows = grid.getSelectedRows();
            if (selectedRows.length == 0) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_MORE_ROW, SQ_type: "warning"});
                return;
            }

            var arr = [];

            for(var index = 0; index < selectedRows.length; index++){
                //客户id
                var cstId = selectedRows[index].cstId;
                //卡号
                var accNo = selectedRows[index].accNo;
                //预留手机号
                var cardTel = selectedRows[index].cardTel;

                var dataStr = "{accNo:"+accNo+",cstId:" + cstId + ",cardTel:" + cardTel +"}";
                arr.push(dataStr);
            }
            var data;
            if(arr != null && arr.length > 0){
                data = "[" + arr.toString() + "]";
            }

            console.log("data = " + data);

            $.ligerDialog.confirm("是否确认发送？", function (flag) {
                if (!flag) return ;

                $.ajax({
                    type: 'post',
                    dataType:'json',
                    data:{"data":data},
                    url: '<%=path%>/custpay/cmmBindingCart/sendSMS.do',
                    beforeSend: function(){
                        wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
                    },
                    success:function(msg){
                        wait_dialog.close();
                        if(msg.success == 1){
                            $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
                            grid.reload();
                        }else{
                            $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
                        }
                    }
                });
            });

        }

        /**
         * 群发短信
         */
        function batchSendSMS() {
            $.ligerDialog.confirm("批量发送短信，请谨慎操作，是否确认发送？", function (flag) {
                if (!flag) return ;

                $.ajax({
                    type: 'post',
                    dataType:'json',
                    data:{"data":null},
                    url: '<%=path%>/custpay/cmmBindingCart/batchSendSMS.do',
                    beforeSend: function(){
                        wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
                    },
                    success:function(msg){
                        wait_dialog.close();
                        if(msg.success == 1){
                            $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
                            grid.reload();
                        }else{
                            $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
                        }
                    }
                });
            });
        }

        //格式化时间
        function renderDate(rowdata, index, value) {
            var dateStr = "";
            if (null != value) {
                var date = new Date(value);
                dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
            }
            return dateStr;
        }

    </script>
</head>

<body class="iframeBody">
<div class="Wrapper">
    <div class="searchArea">
        <form id="searchBar">
            <div class="fieldset1 clearFix">
                <div class="fieldBox">
                    <div class="label">开户名称</div>
                    <div class="field">
                        <input name="accNm" id="accNm" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">预留手机号</div>
                    <div class="field">
                        <input name="cardTel" id="cardTel" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">银行账号</div>
                    <div class="field">
                        <input name="accNo" id="accNo" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">证件号</div>
                    <div class="field">
                        <input name="certNo" id="certNo" type="text" class="ui-textbox" value="">
                    </div>
                </div>

                <div class="clearFix"></div>
                <div class="btnsBox">
                    <span id="searchBtn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_1"></i>查询</span>
                    <span id="clearBtn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_19"></i>清空</span>
                    <span id="sendSMSBtn" class="sq-button sq-button-hasicon" onclick="sendSMS();"><i class="sqBtnIco_19"></i>选发短信</span>
                    <span id="batchSendSMSBtn" class="sq-button sq-button-hasicon" onclick="batchSendSMS();"><i class="sqBtnIco_19"></i>群发短信</span>
                </div>
            </div>
        </form>
    </div>
</div>
<div id="ligergrid1" class="sq-grid"></div>

<div id="change_Html" style="display:none">
    <form id="change_form">
        <div class="fieldset1 tabel_col tabel_col_1 clearFix">
            <div class="fieldBox col_100_auto">
                <div class="label">开户名称</div>
                <div class="field">
                    <input name="accNm" id="accNmEdit" type="text" class="ui-textbox" value="" ligerui="width:250">
                </div>
            </div>
            <div class="fieldBox col_100_auto">
                <div class="label">预留手机号</div>
                <div class="field">
                    <input name="cardTel" id="cardTelEdit" type="text" class="ui-textbox" value="" ligerui="width:250">
                </div>
            </div>
            <div class="fieldBox col_100_auto">
                <div class="label">证件号</div>
                <div class="field">
                    <input name="certNo" id="certNoEdit" type="text" class="ui-textbox" value="" ligerui="width:250">
                </div>
            </div>
        </div>
        <div class="btnsBox">
            <span id="saveChange_Btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_10"></i>保存</span>
        </div>
    </form>
</div>
</body>
</html>
